﻿using Common.Unit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web;
using WcfApp.IService;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using lib.Model;
using lib.BLL;
namespace WcfApp.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]
    public class AccountImpl : IAccount
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        public ResponseData Login(string strData)
        {
            ResponseData result = new ResponseData();
            try
            {
                RequestData requestData = JsonHelper.Deserialize<RequestData>(strData);
                if (requestData != null)
                {
                    string userAccount = requestData["userAccount"];   //账号【手机或邮箱】
                    string userPassword = requestData["userPassword"]; //用户密码

                    Users_Model userModel = Users_Bll.Instance.GetModel("fdUserMobile=@fdUserMobile and fdUserPassword=@fdUserPassword",
                          new SqlParameter("@fdUserMobile", userAccount), new SqlParameter("@fdUserPassword", userPassword));
                    if (userModel != null)
                        result.Data = JsonHelper.Serialize(userModel);
                }
            }
            catch (Exception ex)
            {
                result.ErrMsg = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 注册账号激活,不做判断，直接写入数据
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        public ResponseData Register(string strData)
        {
            ResponseData result = new ResponseData();
            try
            {
                RequestData requestData = JsonHelper.Deserialize<RequestData>(strData);
                if (requestData != null)
                {
                    string userName = requestData["userName"];     //用户名称
                    string userMobile = requestData["userMobile"];     //手机
                    string userPassword = requestData["userPassword"]; //用户密码

                    string account = "user_" + new Random().Next(10000, 99999);
                    Users_Model userModel = new Users_Model()
                    {
                        FdUserAccount = account,
                        FdUserDepName = "",
                        FdUserEmail = "",
                        FdUserMobile = userMobile,
                        FdUserName = userName,
                        FdUserPassword = userPassword,
                        FdUserPasswordSeed = "www"
                    };
                    result.IsSuccess = Users_Bll.Instance.Add(userModel) > 0;
                }
            }
            catch (Exception ex)
            {
                result.ErrMsg = ex.Message;
            }
            return result;
        }
    }
}